package com.generic.mybatis.dal.dao.criteria;

/**
 * 查询条件工厂
 */
public class Criterias {

    private Criterias() {
    }

    public static <T> Criteria eq(String field, T value) {
        return new EqCriteria(field, value);
    }

    public static Criteria ge(String field, Object value) {
        return new GeCriteria(field, value);
    }

    public static Criteria gt(String field, Object value) {
        return new GtCriteria(field, value);
    }

    public static Criteria lt(String field, Object value) {
        return new LtCriteria(field, value);
    }

    public static Criteria le(String field, Object value) {
        return new LeCriteria(field, value);
    }

    public static Criteria ne(String field, Object value) {
        return new NeCriteria(field, value);
    }

    public static Criteria isDelete(boolean isDelete) {
        return new EqCriteria("del_flag", isDelete ? "1" : "0");
    }

    public static Criteria notDel() {
        return new EqCriteria("del_flag", "0");
    }

    public static Criteria like(String field, Object value) {
        return new LikeCriteria(field, value);
    }

    public static Criteria llike(String field, Object value) {
        return new LLikeCriteria(field, value);
    }

    public static Criteria rlike(String field, Object value) {
        return new RLikeCriteria(field, value);
    }

    public static Criteria in(String field, Object... value) {
        return new InCriteria(field, value);
    }

    public static Criteria and(Criteria... criterias) {
        return new AndCriteria(criterias);
    }

    public static Criteria or(Criteria... criterias) {
        return new OrCriteria(criterias);
    }

    public static Criteria notNull(String field) {
        return new NotNullCriteria(field);
    }

    public static Criteria isNull(String field) {
        return new NullCriteria(field);
    }

    public static Criteria inSub(String field, Class subClazz, String targetField, Criteria... criterias) {
        return new InSubSelect(field, subClazz, targetField, criterias);
    }

    public static Criteria inSub(String field, SubQuery subQuery) {
        return new InSubSelect(field, subQuery.getEntityClass(), subQuery.getSelectField(), subQuery.getCriterias());
    }

    public static SubQuery subQuery(Class subClazz, String targetField, Criteria... criterias) {
        return new SubQuery(subClazz, targetField, criterias);
    }

    public static class SubQuery {
        private final Class entityClass;
        private final String selectField;
        private final Criteria[] criterias;

        public SubQuery(Class entityClass, String selectField, Criteria[] criterias) {
            this.entityClass = entityClass;
            this.selectField = selectField;
            this.criterias = criterias;
        }

        public Class getEntityClass() {
            return entityClass;
        }

        public String getSelectField() {
            return selectField;
        }

        public Criteria[] getCriterias() {
            return criterias;
        }
    }

}
